package com.example.maps;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.example.riqrtest.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.graphics.drawable.Drawable;
import android.view.Menu;

/**
 * This is an activity demonstrating the usage of the Google Maps API
 * 
 * This activity creates a basic view containing zooming interface.
 * 
 * Additionally, the example goes over the procedures necessary for adding location points on the map, as well as 
 * requesting location service to query the user's current location.
 * @author Administrator
 *
 */
public class HelloMapsAct extends MapActivity {

	private FixedMyLocationOverlay myLocationOverlay;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello_maps_act);
        
        //Use MapView's zooming capability.
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        
        //Retrieve the MapView's list of locations to be marked, retrieve the sprite for marker.
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable icon = this.getResources().getDrawable(R.drawable.androidmarker);
        HelloItemOverlay itemizedOverlay = new HelloItemOverlay(icon, this);
        
        
        
        
        //Initiate self-location query service
        myLocationOverlay = new FixedMyLocationOverlay (this, mapView);
        mapOverlays.add (myLocationOverlay);
        mapView.postInvalidate();
        
        
        
        
        //Query for a list of locations given the keyword "Dalvik, Iceland"
        List<Address> addrList = searchLocationByName("Dalvik, Iceland", 10);
        //Add all the results onto the map's marker list
        markLocationList(addrList, itemizedOverlay);
        
        //Mark two more locations on the world map.
        markLocation(19240000,-99120000, "Hola, Mundo!", "I'm in Mexico City!", itemizedOverlay);
        markLocation(35410000, 139460000, "Sekai, konichiwa!", "I'm in Japan!", itemizedOverlay);
        
        //Finally, tell the MapView to actually register these points and draw them.
        mapOverlays.add(itemizedOverlay);
    }
    
    /**
     * The app should enable location query services when it is active. 
     */
    @Override
    protected void onResume() {
    	super.onResume();
        myLocationOverlay.enableMyLocation();
    }
    /**
     * Location query service is unnecessary if the app isn't using it.
     */
    @Override
    protected void onPause() {
    	super.onPause();
        myLocationOverlay.disableMyLocation();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    
    
    
    
    /**
     * Using the location query service, search for locations given the keyword contained in the query string.
     * @param query
     * @param maxResult
     * @return
     */
    public List<Address> searchLocationByName(String query, int maxResult) {
        Geocoder decoder = new Geocoder(this, Locale.getDefault());
        List<Address> addressList = new ArrayList<Address>();
        try {
			addressList = decoder.getFromLocationName(query, maxResult);
		} catch (IOException e) {
			// Error obtaining network connection.
			return new ArrayList<Address>(); //Return an empty list
		}
        return addressList; //Return a list of result.
    }
    /**
     * For each address in the list, add the location into the list to be marked onto the map. 
     * @param addrList The list of location not yet added to the marked list.
     * @param itemizedOverlay The list of location to be marked on the map.
     */
    public void markLocationList(List<Address> addrList, HelloItemOverlay itemizedOverlay) {
        for (Address addr : addrList) {
        	GeoPoint addrPoint = new GeoPoint((int) (addr.getLatitude() * 1E6), (int) (addr.getLongitude() * 1E6));
        	OverlayItem overlay = new OverlayItem(addrPoint, "Title", "Candidate");
        	itemizedOverlay.addOverlay(overlay);
        }
    }
    /**
     * Add the location, denoted by a human readable address into the list to be marked onto the map. 
     * @param addr The location not yet added to the marked list.
     * @param itemizedOverlay The list of location to be marked on the map.
     */
    public void markLocation(Address addr, String title, String content, HelloItemOverlay itemizedOverlay) {
        GeoPoint addrPoint = new GeoPoint((int) (addr.getLatitude() * 1E6), (int) (addr.getLongitude() * 1E6));
        OverlayItem overlay = new OverlayItem(addrPoint, title, content);
        itemizedOverlay.addOverlay(overlay);
    }
    /**
     * Add the location, marked by the latitude and longitude on Earth into the list to be marked onto the map. 
     * @param lati The longitude
     * @param longi The latitude
     * @param itemizedOverlay The list of location to be marked on the map.
     */
    public void markLocation(int lati, int longi, String title, String content, HelloItemOverlay itemizedOverlay) {
        GeoPoint addrPoint = new GeoPoint(lati, longi);
        OverlayItem overlay = new OverlayItem(addrPoint, title, content);
        itemizedOverlay.addOverlay(overlay);
    }
}
